define(function(require, exports, module) {

  var Backbone = require('backbone');
  var $ = require('jquery');
  var _ = require('underscore');
  var jsonview = require('jsonview');
  var tmpl = require('text!../tmpl.html');

  require(['jquery', 'moment', 'https://res.wx.qq.com/open/js/jweixin-1.0.0.js'], function($, moment, wx) {
    $(function() {
      alert(wx);
      /*
       * 注意：
       * 1. 所有的JS接口只能在公众号绑定的域名下调用，公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
       * 2. 如果发现在 Android 不能分享自定义内容，请到官网下载最新的包覆盖安装，Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
       * 3. 常见问题及完整 JS-SDK 文档地址：http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
       *
       * 开发中遇到问题详见文档“附录5-常见错误及解决办法”解决，如仍未能解决可通过以下渠道反馈：
       * 邮箱地址：weixin-open@qq.com
       * 邮件主题：【微信JS-SDK反馈】具体问题
       * 邮件内容说明：用简明的语言描述问题所在，并交代清楚遇到该问题的场景，可附上截屏图片，微信团队会尽快处理你的反馈。
       */
      wx.config({
        debug: false,
        signature: "c7a4518ca5cf97d6920876a2afa6807bdeea8823",
        appid: "wxb9d5898cd1142cb4",
        noncestr: "N5jIWtgVuW8SrDvi",
        timestamp: moment().unix(),
        jsApiList: [
          'checkJsApi',
          'onMenuShareTimeline',
          'onMenuShareAppMessage',
          'onMenuShareQQ',
          'onMenuShareWeibo',
          'onMenuShareQZone',
          'hideMenuItems',
          'showMenuItems',
          'hideAllNonBaseMenuItem',
          'showAllNonBaseMenuItem',
          'translateVoice',
          'startRecord',
          'stopRecord',
          'onVoiceRecordEnd',
          'playVoice',
          'onVoicePlayEnd',
          'pauseVoice',
          'stopVoice',
          'uploadVoice',
          'downloadVoice',
          'chooseImage',
          'previewImage',
          'uploadImage',
          'downloadImage',
          'getNetworkType',
          'openLocation',
          'getLocation',
          'hideOptionMenu',
          'showOptionMenu',
          'closeWindow',
          'scanQRCode',
          'chooseWXPay',
          'openProductSpecificView',
          'addCard',
          'chooseCard',
          'openCard'
        ]
      });

      wx.ready(function() {

        var shareData = {
          title: '分享测试', // 分享标题
          desc: '分享测试描述', // 分享描述
          link: 'http://xiongbin.cf/test.html', // 分享链接
          imgUrl: 'http://ubus-service.oss-cn-hangzhou.aliyuncs.com/weixin%2Fimages%2Fhb%2Fshare_icon.png', // 分享图标
          success: function() {
            // 用户确认分享后执行的回调函数
            alert('分享成功');
          },
          cancel: function() {
            // 用户取消分享后执行的回调函数
            alert('取消分享');
          }
        }

        wx.onMenuShareTimeline(shareData);
        wx.onMenuShareAppMessage(shareData);
        wx.onMenuShareQQ(shareData);

      });
    })
  })

  function main() {
    var $app = $('<div id="app">');
    $('#main').html($app);

    $app.append(tmpl);

    require(['jquery'], function($) {
      $('#mylink').html(location.href);

      function touch(event) {
        var event = event || window.event;

        var oInp = document.getElementById("inp");

        switch (event.type) {
          case "touchstart":
            oInp.innerHTML = "Touch started (" + event.touches[0].clientX + "," + event.touches[0].clientY + ")";
            break;
          case "touchend":
            oInp.innerHTML = "<br>Touch end (" + event.changedTouches[0].clientX + "," + event.changedTouches[0].clientY + ")";
            break;
          case "touchmove":
            // event.preventDefault();  
            oInp.innerHTML = "<br>Touch moved (" + event.touches[0].clientX + "," + event.touches[0].clientY + ")";
            break;
        }

      }

      var hstart = 0, hend = 0, top = 0;

      $('body')[0].addEventListener('touchstart', function(eve) {
        top = $(window).scrollTop();
        hstart = eve.touches[0].clientY;
        $('#inp').append('<div>' + eve.type + ' top:' + top + ' start:' + hstart + '</div>');
      });

      $('body')[0].addEventListener('touchmove', function(eve) {
        hend = eve.touches[0].clientY;

        if ($(window).scrollTop() === 0 && hend > hstart)
          eve.preventDefault();
        if (hend - hstart >= top)
          eve.preventDefault();
        $('#inp').append('<div>' + eve.type + ' top:' + top + ' start:' + hstart + ' end:' + hend +'</div>');
        top = $(window).scrollTop();
        hstart = eve.touches[0].clientY;
      });
    });
  }

  return main;
})
